﻿using System;
using System.Diagnostics;
using System.Xml.Linq;

namespace Bing
{
    /// <summary>
    /// Contains properties related to an image, video, or text article returned for a request to the News <see cref="ApiHelper.SourceType"/>.
    /// </summary>
    [DebuggerDisplay("Title: {Title} - {Url}")]
    public class NewsArticle
    {
        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250907.aspx, incorrect Data Type
        /// <summary>
        /// Gets or sets the date on which this article was indexed.
        /// </summary>
        /// <value>The date on which this article was indexed.</value>
        public DateTime Date { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250995.aspx, the remarks are not correct.
        /// <summary>
        /// Gets or sets a representative sample of this article.
        /// </summary>
        /// <value>A representative sample of this article.</value>
        /// <remarks>An article is presented as part of a collection.</remarks>
        public string Snippet { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250995.aspx, the remarks are incorrect.
        /// <summary>
        /// Gets or sets the organization responsible for this article.
        /// </summary>
        /// <value>The organization responsible for this article.</value>
        public string Source { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd250872.aspx, the remarks are incorrect.

        /// <summary>
        /// Gets or sets the headline of this article.
        /// </summary>
        /// <value>The headline of this article.</value>
        public string Title { get; set; }

        // TODO: Documentation Error: http://msdn.microsoft.com/en-us/library/dd251011.aspx, the remarks are incorrect.
        /// <summary>
        /// Gets or sets the URL of this article.
        /// </summary>
        /// <value>The URL of this article.</value>
        public string Url { get; set; }

        /// <summary>
        /// Parses the response from the Bing API.
        /// </summary>
        /// <param name="element">The XML from the Bing API</param>
        /// <returns>An <see cref="NewsArticle" /></returns>
        public static NewsArticle ParseElement(XElement element)
        {
            NewsArticle newsArticle = new NewsArticle();

            newsArticle.Title = ApiHelper.GetElementValueOrDefault<string>(element, "Title", ApiHelper.NewsNs);
            newsArticle.Url = ApiHelper.GetElementValueOrDefault<string>(element, "Url", ApiHelper.NewsNs);
            newsArticle.Source = ApiHelper.GetElementValueOrDefault<string>(element, "Source", ApiHelper.NewsNs);
            newsArticle.Snippet = ApiHelper.GetElementValueOrDefault<string>(element, "Snippet", ApiHelper.NewsNs);
            newsArticle.Date = ApiHelper.GetElementValueOrDefault<DateTime>(element, "Date", ApiHelper.NewsNs);

            return newsArticle;
        }


    }
}
